/**
* @Description : _pagination.js
* @Author : Tomi
* @Email : lintaoming@qq.com
* @Create : 2017/8/31
*/
(function(window, $, undefined){
'use strict';
$(window.document).ready(function(){
    $('[dy8-unit="pagination"]').each(function(i,e){
        var $paginationBox = $(e),
            $pagination = null,
            currentNum = 1,
            lastPageNum = 1;

        var defaults = {
            visiblePages: 5,
            currentPage: currentNum,
            first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
            prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
            next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
            page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>'
        };

        var options = $.extend(defaults, $paginationBox.data());

        function reloadLastPageNum(){
            if(typeof options.totalPages == 'number'){
                lastPageNum = options.totalPages;
            }else if(typeof options.totalCounts == 'number' && typeof options.pageSize == 'number'){
                lastPageNum = Math.ceil(options.totalCounts / options.pageSize);
            }
        }

        reloadLastPageNum();

        if(typeof options.callback != 'undefined' && typeof options.callback == 'string'){
            $.extend(options, {
                onPageChange: function (num, type) {
                    eval('window.'+$paginationBox.data('callback')+'(num,type)');
                }
            });
        }

        if(typeof $paginationBox.data('pagesType') == 'undefined' || $paginationBox.data('pagesType') == 1){
            $pagination = $('<ul />', {
                addClass: 'pagination'
            });
            $paginationBox.append($pagination);
        }

        if($paginationBox.data('pagesType') == 2){
            $pagination = $('<ul />', {
                addClass: 'pagination pagination-extend'
            });
            $paginationBox.append($pagination);
        }

        if(typeof $paginationBox.data('autoPages') == 'boolean' && $paginationBox.data('autoPages') == true){
            var $btnGroup = $('<div />', {
                addClass: 'btn-group',
                html: '<button type="button" class="btn dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
                $paginationBox.data('pageSize')+' 条/页\n' +
                '<span class="caret"></span>\n' +
                '</button>'+
                '<ul class="dropdown-menu">'+
                '<li class="on"><a href="javascript:;">'+$paginationBox.data('pageSize')+' 条/页</a></li>'+
                '<li><a href="javascript:;">'+$paginationBox.data('pageSize') * 2+' 条/页</a></li>'+
                '<li><a href="javascript:;">'+$paginationBox.data('pageSize') * 3+' 条/页</a></li>'+
                '<li><a href="javascript:;">'+$paginationBox.data('pageSize') * 4+' 条/页</a></li>'+
                '</ul>'
            });
            $paginationBox.append($btnGroup);
            $btnGroup.find('li').on('click', function(){
                if($(this).is('.on')) return;
                $(this).addClass('on').siblings('li').removeClass('on');
                $btnGroup.find('.dropdown-toggle').html(
                    $(this).text() +
                    ' <span class="caret"></span>\n'
                );
                $pagination.jqPaginator('destroy');
                $pagination.jqPaginator(
                    $.extend(options,{
                        pageSize: parseInt($(this).text())
                    })
                );
                reloadLastPageNum();
            });
        }
        if(typeof $paginationBox.data('jumpPages') == 'boolean' && $paginationBox.data('jumpPages') == true){
            var $jump = $('<div />', {
                addClass: 'pagination-jump',
                html: '<span class="pagination-tips">前往</span>\n' +
                '<input type="text" class="form-control" />\n' +
                '<span class="pagination-tips">页</span>\n'
            });
            $paginationBox.append($jump);
            $jump.find('input').on('keydown', function(ev){
                var pageNum = parseInt($(this).val());
                if(ev.keyCode==13){
                    if(pageNum < 0 || isNaN(pageNum)) {
                        pageNum = 1;
                    }else if(pageNum > lastPageNum){
                        pageNum = lastPageNum;
                    }
                    $pagination.jqPaginator('destroy');
                    $pagination.jqPaginator(
                        $.extend(options,{
                            currentPage: pageNum
                        })
                    );
                }
            });
        }
        $pagination.jqPaginator(defaults);
    });
});
})(window, jQuery);